package com.simplefan.framework.common.rest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RestObjectAnalyzerFactory {

  public static RestParameterAnalyzer getRestParameterAnalyzer() {
    return new RestParameterAnalyzer();
  }

  public static RestParameterAnalyzer getRestParameterAnalyzer(String xmlFragment) {
    return new RestParameterAnalyzer(xmlFragment);
  }

  public static RestResultAnalyzer getRestResultAnalyzer() {
    return new RestResultAnalyzer();
  }

  public static RestResultAnalyzer getRestResultAnalyzer(String xmlFragment) {
    return new RestResultAnalyzer(xmlFragment);
  }

  @SuppressWarnings("unchecked")
  public static RestObjectAnalyzer getAnalyzer(String typeName) {
    return restObjectAnalyzers.get(typeName);
  }

  public static RestListAnalyzer getListAnalyzer() {
    return restListAnalyzer;
  }

  public static String getTypeNameByClassName(String name) {
    return classToType.get(name);
  }

  public static String getClassNameByTypeName(String name) {
    return typeToClass.get(name);
  }

  public static void registerRestListAnalyzer(RestListAnalyzer regiestRestListAnalyzer) {
    restListAnalyzer = regiestRestListAnalyzer;
    typeToClass.put("list", List.class.getName());
    classToType.put(List.class.getName(), "list");
  }

  @SuppressWarnings("unchecked")
  public static void registerRestObjectAnalyzer(String typeName, String className, RestObjectAnalyzer restObjectAnalyzer) {
    if(restObjectAnalyzers == null) {
      restObjectAnalyzers = new HashMap<String, RestObjectAnalyzer>();
    }

    restObjectAnalyzers.put(typeName, restObjectAnalyzer);
    typeToClass.put(typeName, className);
    classToType.put(className, typeName);
  }

  @SuppressWarnings("unchecked")
  private static Map<String, RestObjectAnalyzer> restObjectAnalyzers;

  private static RestListAnalyzer restListAnalyzer;

  private static Map<String, String> typeToClass = new HashMap<String, String>();

  private static Map<String, String> classToType = new HashMap<String, String>();
}
